/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int depth(TreeNode* root,vector<int>& rem){
        if(root==NULL) return 0;
        else{
            int l=depth(root->left,rem)+1;
            int r=depth(root->right,rem)+1;
            rem.emplace_back(l+r-2);
            return max(l,r);
        }
    }
    int diameterOfBinaryTree(TreeNode* root) {
        vector<int> rem;
        depth(root,rem);
        int max=INT_MIN;
        for(int i=0;i<rem.size();i++){
            if(rem[i]>max)
                max=rem[i];
        }
        return max;
    }
};
